package com.spring.sso.runner;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.spring.sso.common.CommonResult;
import com.spring.sso.config.common.AuthenticationConfig;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.*;

/**
 * @author : pangfuzhong
 * @description
 * @date : 2021/10/23 11:05
 */
@Slf4j
@Component
public class ApplicationContextRunner implements ApplicationRunner, ApplicationContextAware {
    private static ApplicationContext applicationContext;
    private static final CountDownLatch countDownLatch = new CountDownLatch(1);

    @Autowired
    private AuthenticationConfig authenticationConfig;

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        applicationContext = context;
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        //this.validGatewayRequestLimiter();
    }

    private void validGatewayRequestLimiter() throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        for(int i = 0; i < 1; i++) {
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        RestTemplate restTemplate = applicationContext.getBean(RestTemplate.class);
                        HttpHeaders headers = new HttpHeaders();
                        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

                        LinkedMultiValueMap<String, String> bodyMap = new LinkedMultiValueMap<>();
                        bodyMap.put("userName", Lists.newArrayList("spring")); bodyMap.put("password", Lists.newArrayList("123"));

                        HttpEntity<LinkedMultiValueMap<String, String>> requestEntity = new HttpEntity<LinkedMultiValueMap<String, String>>(bodyMap, headers);
                        String url = "http://192.168.0.104:44678/gateway/downstream/concret";
                        log.info("--> 线程 {} 即将发起请求, path = {}", Thread.currentThread().getName(), url);

                        countDownLatch.await();

                        ResponseEntity<CommonResult> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, CommonResult.class);
                        log.info("--> 线程 {} 接受到返回结果, result = {}", Thread.currentThread().getName(), responseEntity.getBody().toString());

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }

        log.info("--> 10秒后 {} 将并发发送进行请求");
        TimeUnit.MILLISECONDS.sleep(10000);
        countDownLatch.countDown();
    }
}
